<?

require_once('DataObject/DomainModel.php');
require_once('Languages.php');
require_once('LocaleDefaults.php');

class LocaleTranslations extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $languagesId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=no
	protected $localeDefaultsId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $translation;	//primaryKey=no, dataType=text, maxLength=65535, allowNulls=no
}

class LocaleTranslationsMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'locale_translations';
		$this->selectViewName = 'v_locale_translations';
		$this->selectFields = '
			id,
			languages_id,
			locale_defaults_id,
			translation,
			ld_id,
			ld_languages_id,
			ld_locale_sections_id,
			ld_translation,
			l_id,
			l_language,
			l_shortcut
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_locale_translations (@p_id,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_locale_translations (
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_locale_translations (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setLanguagesId($array['languages_id']);
		$obj->setLocaleDefaultsId($array['locale_defaults_id']);
		$obj->setTranslation($array['translation']);

		$arr = ArrayUtils::splitByPrefix($array, array('ld_','l_'));
				
		if (!empty($arr['l_']))
			ModelMapper::instance('Languages')->loadArray($arr['l_']);
		if (!empty($arr['ld_']))
			ModelMapper::instance('LocaleDefaults')->loadArray($arr['ld_']);
		
		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getLanguagesId(),
			$obj->getLocaleDefaultsId(),
			$obj->getTranslation()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getLanguagesId(),
			$obj->getLocaleDefaultsId(),
			$obj->getTranslation()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE locale_translations');
		return $row['Auto_increment'];

	}
	
	function getPhrasesToTranslate($languagesId = 0, $filter = '', $orderBy = ''){
		if (intval($languagesId > 0)){
			$and = "AND lt.languages_id = ".$languagesId."";
		}
		
		$query = "
			SELECT
				ld.id,
				ld.languages_id,
				ld.locale_sections_id,
				ld.translation,
				ld.ls_id,
				ld.ls_section,
				l.id as l_id,
				l.language as l_language,
				l.shortcut as l_shortcut,
				lt.id as lt_id,
				lt.languages_id as lt_languages_id,
				lt.locale_defaults_id as lt_locale_defaults_id,
				lt.translation as lt_translation
			FROM v_locale_defaults ld
			LEFT JOIN languages l ON l.id = ld.languages_id
			LEFT JOIN v_locale_translations lt ON ld.id = lt.locale_defaults_id ".$and."
			".$filter."
			".$orderBy."
		";
		
		return self::$DB->getAll($query);
		
	}
}

class LocaleTranslationsCollection extends ModelCollection {
	function add(LocaleTranslations $model){
		$this->doAdd($model);
	}
}

class LocaleTranslationsCollectionExtended extends LocaleTranslationsCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>